<?php
/**
 * The Url class
 *
 * @todo more url pattern
 */
class Url
{
	/**
	 * The app bootstrap file
	 * @var string
	 */
	static public $bootstrapFile;
	/**
	 * The url prefix
	 * @var string
	 */
	static public $prefix;

	/**
	 * The url pattern
	 * 
	 * @var string 
	 */
	static public $urlPattern;
	

	/**
	 * Generate url string
	 * 
	 * @param string $module
	 * @param string $action
	 * @param array $args 
	 * @param array $options 
	 * @example generate('adress', 'set', array('id' => 1), array('pattern' => 'rewrite', 'encoded' => false))
	 */
	static public function generate($module, $action, $args = null, $options = null)
	{
		$pattern = isset($options['pattern']) ? $options['pattern'] : self::$urlPattern;
		switch ($pattern)
		{
			case 'standard':
			default:// URL format: ?module=module_name&action=action_name&key=value
				$url = self::$bootstrapFile . "?module=$module&action=$action";
				if ($args)
				{
					foreach($args as $key => $value)
					{
						if (isset($options['encoded']) && true == $options['encoded'])
						{
							$value = urlencode($value);
						}
						$url .= "&$key=$value";
					}
				}
				break;
			case 'rewrite':// URL formar: module_name/action_name?key=value
				$url = "$module/$action";
				if ($args)
				{
					$queryString = '';
					foreach($args as $key => $value)
					{
						if (isset($options['encoded']) && true == $options['encoded'])
						{
							$value = urlencode($value);
						}
						$queryString .= "&$key=$value";
					}
					$url .= '?' . substr($queryString, 1);
				}
				break;
			case 'path_info':// URL formar: module_name/action_name/key/value
				$url = self::$bootstrapFile . "/$module/$action";
				if ($args)
				{
					foreach($args as $key => $value)
					{
						if (isset($options['encoded']) && true == $options['encoded'])
						{
							$value = urlencode($value);
						}
						$url .= "/$key/$value";
					}
				}
				break;
		}
		return self::$prefix . $url;
	}
}
